//package cn.maihe.elg.operation.centers.epoint.sanming.handler;
//
//import cn.maihe.elg.operation.centers.epoint.sanming.dto.EpointBaseRespDTO;
//import cn.maihe.elg.operation.centers.epoint.sanming.enums.EpointRespCodeEnum;
//import cn.maihe.elg.operation.centers.epoint.sanming.exception.EpointException;
//import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
//import cn.maihe.elg.operation.exception.CenterException;
//import cn.maihe.elg.operation.exception.ExceptionUtils;
//import cn.maihe.elg.operation.model.enums.CenterNoEnum;
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.annotation.AnnotationUtils;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.context.request.async.DeferredResult;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.util.WebUtils;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.net.SocketTimeoutException;
//import java.util.Collection;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.concurrent.CompletableFuture;
//
///**
// * @Description 龙岩公共资源服务中心-金融机构直开式
// * @Author WJH
// * @Date 2021/04/19
// */
//@RestController
//@RequestMapping(value = "/v1/center/" + LongYanEpointCenterHandler.centerNo)
//@Slf4j
//public class LongYanEpointCenterHandler {
//    /**
//     * FIXME 固定后管初始化值
//     * 中心编号：取值CenterNoEnum.LONGYAN.getCenterNo
//     */
//    private final static CenterNoEnum centerNoEnum = CenterNoEnum.LONGYAN;
//    public static final String centerNo = "C0597";
//
//    private final Map<String, EpointCenterTradeService> tradeServiceMap = new HashMap<>();
//
//
//    public LongYanEpointCenterHandler(@SuppressWarnings("rawtypes") final Collection<EpointCenterTradeService> baseCenterServices) {
//        super();
//        baseCenterServices.forEach(item -> {
//            CenterTradeService ts = AnnotationUtils.findAnnotation(item.getClass(), CenterTradeService.class);
//            if (centerNoEnum == ts.centerNo()) {
//                tradeServiceMap.put(ts.tradeType().name().toLowerCase(), item);
//            }
//        });
//    }
//
//    @PostMapping(value = "/{orgCode}/{tradeType}", consumes = {"application/x-www-form-urlencoded"})
//    public @ResponseBody
//    DeferredResult<Object> service(@PathVariable("orgCode") String orgCode, @PathVariable("tradeType") final String tradeType, final HttpServletRequest req, final HttpServletResponse resp) {
//        Map<String, Object> parameters = WebUtils.getParametersStartingWith(req, "");
//        return this.doService(orgCode, tradeType, req, resp, parameters, null);
//    }
//
//    @PostMapping(value = "/{orgCode}/{tradeType}", consumes = {"application/json", "application/xml"})
//    public @ResponseBody
//    DeferredResult<Object> service(@PathVariable("orgCode") String orgCode, @PathVariable("tradeType") final String tradeType, @RequestBody final Map<String, Object> map, final HttpServletRequest req, final HttpServletResponse resp) {
//        return this.doService(orgCode, tradeType, req, resp, map, null);
//    }
//
//
//    @SuppressWarnings("unchecked")
//    protected DeferredResult<Object> doService(String orgCode, String tradeType, final HttpServletRequest req, final HttpServletResponse resp, final Map<String, Object> parameters, final Map<String, MultipartFile> multipartFileMap) {
//
//        DeferredResult<Object> dr = new DeferredResult<Object>();
//        if (!this.tradeServiceMap.containsKey(tradeType.toLowerCase())) {
//            dr.setResult(buildRespException(EpointRespCodeEnum.FAILURE, "不支持的交易类型"));
//            return dr;
//        }
//
//        CompletableFuture
//                .supplyAsync(() -> {
//                    try {
//                        return this.tradeServiceMap.get(tradeType.toLowerCase()).doService(orgCode, parameters, multipartFileMap);
//                    } catch (Exception e) {
//                        throw buildEpointException(e);
//                    }
//                })
//                .whenCompleteAsync((baseResp, t) -> {
//                    if (t != null) {
//                        EpointException te = buildEpointException(t);
//                        log.error("中心：{},交易类型：{},服务错误：{}", centerNoEnum.getCenterName(), tradeType, t.getMessage(), t);
//                        EpointBaseRespDTO respDTO = buildRespException(te.getStatusEnum(), te.getMessage());
//                        dr.setResult(respDTO);
//                    } else {
//                        dr.setResult(baseResp);
//                    }
//                    log.info("中心：{},交易类型：{},响应报文：{}", centerNoEnum.getCenterName(), tradeType, JSON.toJSONString(dr.getResult()));
//                });
//        return dr;
//    }
//
//    public EpointException buildEpointException(CenterException ce) {
//        EpointRespCodeEnum respCodeEnum = EpointRespCodeEnum.FAILURE;
//        //待优化明细
//        if (ce.getInnerRespCode().isFailure()) {
//            respCodeEnum = EpointRespCodeEnum.FAILURE;
//        } else if (ce.getInnerRespCode().isException()) {
//            respCodeEnum = EpointRespCodeEnum.FAILURE;
//        } else if (ce.getInnerRespCode().isTimeOut()) {
//            respCodeEnum = EpointRespCodeEnum.FAILURE;
//        }
//        return EpointException.of(respCodeEnum, ce.getMessage());
//    }
//
//    private EpointBaseRespDTO buildRespException(EpointRespCodeEnum statusEnum, String msg) {
//        return EpointBaseRespDTO.of(statusEnum, msg);
//    }
//
//    private EpointException buildEpointException(Throwable t) {
//        EpointException te = ExceptionUtils.isAssignableFrom(EpointException.class, t);
//        if (te == null) {
//            CenterException ce = ExceptionUtils.isAssignableFrom(CenterException.class, t);
//            if (ce != null) {
//                te = buildEpointException(ce);
//            } else {
//                SocketTimeoutException timeout = ExceptionUtils.isAssignableFrom(SocketTimeoutException.class, t);
//                if (timeout != null) {
//                    te = EpointException.of(EpointRespCodeEnum.FAILURE, "请求超时", t);
//                } else {
//                    te = EpointException.error("请求错误:" + t.getMessage(), t);
//                }
//            }
//        }
//        return te;
//    }
//
//
//}
